package com.tmall.tmall.mapper;

import com.tmall.tmall.pojo.Product;
import com.tmall.tmall.pojo.ProductRt;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into product (cid, name, sub_title, price, stock, create_date) value (#{cid}, #{name}, #{subTitle}, #{price}, #{stock}, #{createDate})")
    void insert(Product product);

    List<ProductRt> page(String name, Integer cid,Integer qwe);

    @Select("select p.id as id," +
            "p.name as name," +
            "p.cid as cid," +
            "pi.name as mainImg," +
            "p.price as price," +
            "p.stock as stock," +
            "p.sub_title as subTitle," +
            "p.sale_count as saleCount" +
            " from product as p,product_image as pi where p.delete_at is null and pi.delete_at is null and p.id=#{id} and p.id=pi.pid and pi.type='mainImg'")
    ProductRt getById(Integer id);

    void update(Product product);

    @Select("select p.id as id," +
            "p.name as name," +
            "p.cid as cid," +
            "pi.name as mainImg," +
            "p.price as price," +
            "p.stock as stock," +
            "p.sub_title as subTitle," +
            "p.sale_count as saleCount" +
            " from product as p,product_image as pi where p.delete_at is null and pi.delete_at is null and p.id=pi.pid and pi.type='mainImg' and p.stock>0")
    List<ProductRt> selectAll();

    @Select("select name from product where delete_at is null and id=#{id}")
    String getNameById(Integer id);

    @Update("update product set stock=stock-#{number},sale_count=sale_count+#{number} where id=#{pid}")
    void updateSS(Integer pid, Integer number);
    
    @Select("select price from product where delete_at is null and id=#{id}")
    java.math.BigDecimal getPriceById(Integer id);
}
